package org.hefan.org.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hefan.basic.exception.BusinessException;
import org.hefan.basic.util.AjaxResult;
import org.hefan.basic.util.PageList;
import org.hefan.org.domain.Shop;
import org.hefan.org.query.ShopQuery;
import org.hefan.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 店铺接口类
 */
@RestController
@RequestMapping("/shop")
@Api(tags = "店铺接口",description = "店铺接口详细描述")
public class ShopController {

    /**
     * 注入shopService
     */
    @Autowired
    private IShopService shopService;

    /**
     * 查询全部
     * //处理请求：Get http://localhost:8080/shop
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部店铺",notes = "")
    public List<Shop> findAll() {
        return shopService.loadAll();
    }

    /**
     * 查询单个Shop
     * 处理请求：Get http://localhost:8080/shop/1
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个店铺",notes = "需要传入id")
    public Shop findOne(@PathVariable("id")Long id) {
        return shopService.loadById(id);
    }

    /**
     * 分页查询
     * post请求,参数在请求体中,通过@RequestBody获取请求体中的参数
     * @param shopQuery 分页查询参数
     * @return
     */
    @PostMapping
    @ApiOperation(value = "通过分页查询店铺",notes = "需要传入分页数据")
    public PageList<Shop> queryPage(@RequestBody ShopQuery shopQuery) {
        return shopService.queryPage(shopQuery);
    }

    /**
     * 删除店铺
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除店铺",notes = "通过id删除")
    public AjaxResult delete(@PathVariable("id")Long id) {
        try {
            shopService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 添加或修改店铺
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改店铺",notes = "有id则为修改,没有id则是添加")
    public AjaxResult saveOrUpdate(@RequestBody Shop shop) {
        try {
            if (shop.getId() == null) {
                shopService.save(shop);
            } else {
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    /**
     * 批量删除店铺
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除店铺",notes = "传需要批量删除的店铺id")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            shopService.batchRemove(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        }
    }

    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻接口",notes = "")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) {
            e.printStackTrace();
            return new AjaxResult(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "系统错误，" + e.getMessage());
        }
    }
}
